﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace HamcoWcs.Tools.Tibco
{
    public class TibcoConfigurationManager
    {
        public static TibcoConfiguration DefaultConfiguration { get; private set; }

        public static TibcoConfigurationManager Manager { get; private set; }

        public static TibcoConfiguration Configuration { get; private set; }

        static TibcoConfigurationManager()
        {
            DefaultConfiguration = new TibcoConfiguration
            {
                Environment = "default",
                Service = "7500",
                Network = "IP00",
                Daemon = "192.168.10.18:7500",
                SendField = "xmlData",
                SendSubject = "CSOT.T7.MES.T7_DEV.FAB.AGVsvr",
                RecvSubject = "CSOT.T7.MES.T7_DEV.FAB.AGVsvr",
                SendTimeout = 5,
                IsBuildWithVirtualTibcoServer = true,
            };
            var configStr = File.ReadAllText("tibco.config.json");
            var manager = JsonConvert.DeserializeObject<TibcoConfigurationManager>(configStr);
            if (manager == null) manager = new TibcoConfigurationManager();
            Manager = manager;
            Configuration = Manager.GetCurrentConfiguration();
        }
        public string CurrentEnvironment { get; set; }

        public List<TibcoConfiguration> Configurations { get; set; }

        public TibcoConfiguration GetCurrentConfiguration()
        {
            var cnf = Configurations.FirstOrDefault(x => x.Environment.ToUpper() == CurrentEnvironment.ToUpper());
            if (cnf == null) cnf = DefaultConfiguration;
            return cnf;
        }
    }

    public class TibcoConfiguration
    {
        //static TibcoConfiguration()
        //{
        //    var configStr = File.ReadAllText("tibco.config.json");
        //    var config = JsonConvert.DeserializeObject<TibcoConfiguration>(configStr);
        //    if (string.IsNullOrEmpty(config.SendField)) config.SendField = "xmlData";
        //    BasicConfiguration = config;
        //}

        //public static TibcoConfiguration BasicConfiguration { get; private set; }
        public string Environment { get; set; }
        public string Daemon { get; set; }
        public string Network { get; set; }
        public string SendField { get; set; } = "xmlData";
        public double SendTimeout { get; set; } = 5;
        public string Service { get; set; }
        public string SendSubject { get; set; }
        public string RecvSubject { get; set; }
        public string LicenseTicket { get; set; }
        public string EventUser { get; set; }
        public bool IsBuildWithVirtualTibcoServer { get; set; } = false;


    }
}